#include "Geometry.h"
#include <qnamespace.h>

#include <QtDebug>

#include <cmath>

using namespace Model;

Vertex::Vertex()
    : _x(0), _y(0)
{       }

Vertex::Vertex(double x, double y)
    : _x(x), _y(y)
{       }

double Vertex::x() const
{
    return _x;
}

double Vertex::y() const
{
    return _y;
}

void Vertex::move(double x, double y)
{
    _x = x;
    _y = y;
}

bool Vertex::isNear(double x, double y)
{
    return (abs(x - _x) < 4 && abs(y - _y) < 4);
}

Edge::Edge(Vertex* start, Vertex* end)
    : _start(start), _end(end)
{       }

Edge::Edge()
{
}

const Vertex* Model::Edge::start() const
{
    return _start;
}

const Vertex* Model::Edge::end() const
{
    return _end;
}

void Model::Edge::cut(Model::Edge* source, double x, double y, Model::Edge** first, Model::Edge** second, Vertex** cutpoint)
{
    Vertex* start = source->_start;
    Vertex* end = source->_end;
    
    (*cutpoint) = new Vertex(x, y);
    (*first) = new Model::Edge(start, (*cutpoint));
    (*second) = new Model::Edge((*cutpoint), end);
    
    delete source;
}

bool Model::Edge::hasEnd(Vertex* v) const
{
    return _start == v || _end == v;
}

bool Edge::isNear(double xc, double yc)
{
    float xa = _start->x(), ya = _start->y(), xb = _end->x(), yb = _end->y();
    float d = abs((xc - xa)*(yb - ya) - (xb - ya)*(yc - xa)) / sqrt((yb - ya)*(yb - ya) + (xb - xa)*(xb - xa));
    return d < 5;
}


Mesh::Mesh()
{       }

QList<Model::Edge*>* Mesh::edges()
{
    return &_edges;
}

QList<Vertex*>* Mesh::vertexes()
{
    return &_vertexes;
}

